package com.tas.manager;

import com.tas.anno.AsyncMethod;
import com.tas.pojo.AsyncMethodHolder;
import org.noear.snack.ONode;
import org.noear.solon.core.BeanExtractor;
import org.noear.solon.core.BeanWrap;

import java.lang.reflect.Method;

/**
 * @author kevin
 */
public class AsyncExtractor  implements BeanExtractor<AsyncMethod> {


  final AsyncMethodManager manager;
  public AsyncExtractor(AsyncMethodManager manager) {
    this.manager = manager;
  }

  @Override
  public void doExtract(BeanWrap bw, Method method, AsyncMethod asyncMethod) throws Throwable {
    AsyncMethodHolder methodHolder = new AsyncMethodHolder(method, bw);
    methodHolder.setMethod(method);
    methodHolder.setBean(bw);
    //校验输出参数
    Class<?> returnType = method.getReturnType();
    if(false == Void.class.isAssignableFrom(returnType)
      && false == void.class.isAssignableFrom(returnType)){
      throw new RuntimeException("AsyncMethod必须返回void，方法："
        + bw.raw(true).getClass().getName() + "#" + method.getName());
    }
    manager.addMethod(methodHolder);
  }
}
